var rosObj = null

var rosUrl = ""

var topic = {
  result: null,
  controlModelStatus: null,
  controlDoor: null,
  roadPlan: null,
  taskStatus: null,
  oneButtonTaskStatus: null,
  YXYCdata: null,
  weatherInfo: null,
  taskExecuteStatus: null,
  inspectedResultWeb: null,
  sysAlarmWeb: null,
  taskRealStatusWeb: null,
  taskPlanStatus: null,
}

var callBackFun = {
  result: null,
  controlModelStatus: null,
  taskStatus: null,
  oneButtonTaskStatus: null,
  YXYCdata: null,
  weatherInfo: null,
  taskExecuteStatus: null,
  inspectedResultWeb: null,
  sysAlarmWeb: null,
  taskRealStatusWeb: null,
  taskPlanStatus: null,
}

// 心跳状态 1 存在 0 不存在
var heartStatus = 0;

exports.install = function (Vue, options) {
  Vue.prototype.rosInit = function (data) {
    console.log("ros init");
    rosObj = new ROSLIB.Ros();
    monitorsHeart();
    return rosObj;
  }

  Vue.prototype.rosConnect = function (data) {
    if (data && data.url) {
      console.log("ros connect");
      rosObj.connect(data.url);
      rosUrl = data.url
      return true;
    } else {
      return false;
    }
  }

  Vue.prototype.rosClose = function (data) {
    console.log("ros close");
    rosObj.close();
    return true;
  }

  Vue.prototype.rosConnectStatus = function (data) {
    console.log("ros connected");
    return rosObj;
  }

  Vue.prototype.rosOnConnection = function (data) {
    rosObj.on("connection", function () {
      console.log("ros connection");
      rosUnsubscribes()
      rosSubscribes()
    });
    return true;
  }

  Vue.prototype.rosOnClose = function (data) {
    rosObj.on("close", function () {
      console.log("ros close");
    });
    return true;
  }

  Vue.prototype.rosOnError = function (data) {
    rosObj.on("error", function (error) {
      console.log("ros error");
    });
    return true;
  }

  /**				
   *   结果上传模块回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.resultCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.result = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.resultUnCallBack = function () {
    callBackFun.result = null;
  }

  /**				
   *   控制模式切换回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.controlModelStatusCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.controlModelStatus = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.controlModelStatusUnCallBack = function () {
    callBackFun.controlModelStatus = null;
  }

  /**				
   *   任务执行过程中的状态回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.taskStatusCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.taskStatus = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.taskStatusUnCallBack = function () {
    callBackFun.taskStatus = null;
  }

  /**				
   *   一键返航任务开始结束消息回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.oneButtonTaskStatusCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.oneButtonTaskStatus = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.oneButtonTaskStatusUnCallBack = function () {
    callBackFun.oneButtonTaskStatus = null;
  }

  /**				
   *   电量上报回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.YXYCdataCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.YXYCdata = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.YXYCdataUnCallBack = function () {
    callBackFun.YXYCdata = null;
  }

  /**				
   *   充电状态回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.weatherInfoCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.weatherInfo = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.weatherInfoUnCallBack = function () {
    callBackFun.weatherInfo = null;
  }

  /**				
   *   任务执行状态回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.taskExecuteStatusCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.taskExecuteStatus = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.taskExecuteStatusUnCallBack = function () {
    callBackFun.taskExecuteStatus = null;
  }

  /**				
   *   巡检点结果(传给前端)回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.inspectedResultWebCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.inspectedResultWeb = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.inspectedResultWebUnCallBack = function () {
    callBackFun.inspectedResultWeb = null;
  }

  /**				
   *   机器人报警结果(传给前端)回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.sysAlarmWebCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.sysAlarmWeb = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.sysAlarmWebUnCallBack = function () {
    callBackFun.sysAlarmWeb = null;
  }

  /**				
   *   任务状态结果(传给前端)回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.taskRealStatusWebCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.taskRealStatusWeb = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.taskRealStatusWebUnCallBack = function () {
    callBackFun.taskRealStatusWeb = null;
  }

  /**				
   *   路径规划状态返回回调				
   *				
   *   @param {*} callBack 回调函数				
   */
  Vue.prototype.taskPlanStatusCallBack = function (callBack) {
    if (callBack instanceof Function) {
      callBackFun.taskPlanStatus = callBack
    } else {
      callBack({
        status: 1001,
        message: "错误：指定类型为Function"
      })
    }
  }
  Vue.prototype.taskPlanStatusUnCallBack = function () {
    callBackFun.taskPlanStatus = null;
  }


  /**				
   *   发送数据库更新表名			
   *				
   *   @param {*} parameter 发送的参数				
   */
  Vue.prototype.dbTableUpdatePub = function (parameter) {
    if (topic.dbTableUpdate) {
      var twist = new ROSLIB.Message(parameter);
      topic.dbTableUpdate.publish(twist);
      return 0
    } else {
      return -1;
    }
  }


  /**				
   *   路径规划服务(service)				
   *				
   *   @param {*} parameter 发送的参数
   *   @param {*} callback	服务回到函数		
   */
  Vue.prototype.roadPlanCallSeavice = function (parameter, callback) {
    if (topic.roadPlan) {
      var require = new ROSLIB.ServiceRequest(parameter);
      topic.roadPlan.callService(require, callback)
    }
  }

  /**				
   *   门控制服务(service)				
   *				
   *   @param {*} parameter 发送的参数
   *   @param {*} callback	服务回到函数		
   */
  Vue.prototype.controlDoorCallSeavice = function (parameter, callback) {
    if (topic.controlDoor) {
      var require = new ROSLIB.ServiceRequest(parameter);
      topic.controlDoor.callService(require, callback)
    }
  }
};

/**
 *  订阅所有
 *
 */
function rosSubscribes() {
  rosOneButtonTaskStatusSubscribe();
  rosTaskExecuteStatusSubscribe();
  rosInspectedResultWebSubscribe();
  rosSysAlarmWebSubscribe();
  rosTaskRealStatusWebSubscribe();
  rosTaskPlanStatusSubscribe();
  rosControlModelStatusSubscribe();
}

/**
 *  取消订阅所有
 *
 */
function rosUnsubscribes() {
  rosOneButtonTaskStatusUnsubscribe();
  rosTaskExecuteStatusUnsubscribe();
  rosInspectedResultWebUnsubscribe();
  rosSysAlarmWebUnsubscribe();
  rosTaskRealStatusWebUnsubscribe();
  rosTaskPlanStatusUnsubscribe();
  rosControlModelStatusUnsubscribe();
}


/**				
 *   结果上传模块声明和订阅				
 *	 暂时没有使用
 */
function rosResultSubscribe() {
  topic.result = new ROSLIB.Topic({
    ros: rosObj,
    name: "/detect_result",
    messageType: "yidamsg/InspectedResult"
  });
  topic.result.subscribe(message => {
    if (callBackFun.result) {
      callBackFun.result(message)
    }
  });
}

/** 				
 *   结果上传模块取消订阅				
 *	 暂时没有使用
 */
function rosResultUnSubscribe() {
  if (topic.result) {
    topic.result.unsubscribe();
  }
}


/**				
 *   门控制声明和订阅				
 *	 暂时没有使用
 */
function rosControlDoorSubscribe() {
  topic.controlDoor = new ROSLIB.Service({
    ros: rosObj,
    name: "/control_door",
    serviceType: "Yidamsg/ControlDoor"
  });
}


/**				
 *   任务执行过程中的状态声明和订阅				
 *	 暂时没有使用
 */
function rosTaskStatusSubscribe() {
  topic.taskStatus = new ROSLIB.Topic({
    ros: rosObj,
    name: "/task_status",
    messageType: "yidamsg/task_status"
  });
  topic.taskStatus.subscribe(message => {
    if (callBackFun.taskStatus) {
      callBackFun.taskStatus(message)
    }
  });
}

/** 				
 *   任务执行过程中的状态取消订阅				
 *	 暂时没有使用
 */
function rosTaskStatusUnsubscribe() {
  if (topic.taskStatus) {
    topic.taskStatus.unsubscribe();
  }
}


/**				
 *   一键返航任务开始结束消息声明和订阅				
 *				
 */
function rosOneButtonTaskStatusSubscribe() {
  topic.oneButtonTaskStatus = new ROSLIB.Topic({
    ros: rosObj,
    name: "/onebuttontask_status",
    messageType: "yidamsg/taskPlanStatus"
  });
  topic.oneButtonTaskStatus.subscribe(message => {
    if (callBackFun.oneButtonTaskStatus) {
      callBackFun.oneButtonTaskStatus(message)
    }
  });
}

/** 				
 *   一键返航任务开始结束消息取消订阅				
 *				
 */
function rosOneButtonTaskStatusUnsubscribe() {
  if (topic.oneButtonTaskStatus) {
    topic.oneButtonTaskStatus.unsubscribe();
  }
}


/**				
 *   电量上报声明和订阅				
 *   暂时没有使用
 */
function rosYXYCdataSubscribe() {
  topic.YXYCdata = new ROSLIB.Topic({
    ros: rosObj,
    name: "/robot_yxycdata",
    messageType: "Yidamsg/nrcar_status"
  });
  topic.YXYCdata.subscribe(message => {
    if (callBackFun.YXYCdata) {
      callBackFun.YXYCdata(message)
    }
  });
}

/** 				
 *   电量上报取消订阅				
 *   暂时没有使用			
 */
function rosYXYCdataUnsubscribe() {
  if (topic.YXYCdata) {
    topic.YXYCdata.unsubscribe();
  }
}


/**				
 *   充电状态声明和订阅				
 *   暂时没有使用
 */
function rosWeatherInfoSubscribe() {
  topic.weatherInfo = new ROSLIB.Topic({
    ros: rosObj,
    name: "/weather_info",
    messageType: "Yidamsg/weather"
  });
  topic.weatherInfo.subscribe(message => {
    if (callBackFun.weatherInfo) {
      callBackFun.weatherInfo(message)
    }
  });
}

/** 				
 *   充电状态取消订阅				
 *	 暂时没有使用		
 */
function weatherInfoUnsubscribe() {
  if (topic.weatherInfo) {
    topic.weatherInfo.unsubscribe();
  }
}


/**				
 *   任务执行状态声明和订阅				
 *				
 */
function rosTaskExecuteStatusSubscribe() {
  topic.taskExecuteStatus = new ROSLIB.Topic({
    ros: rosObj,
    name: "/task_execute_status",
    messageType: "yidamsg/TaskExecuteStatus"
  });
  topic.taskExecuteStatus.subscribe(message => {
    if (callBackFun.taskExecuteStatus) {
      callBackFun.taskExecuteStatus(message)
    }
  });
}

/** 				
 *   任务执行状态取消订阅				
 *				
 */
function rosTaskExecuteStatusUnsubscribe() {
  if (topic.taskExecuteStatus) {
    topic.taskExecuteStatus.unsubscribe();
  }
}


/**				
 *   巡检点结果(传给前端)声明和订阅				
 *				
 */
function rosInspectedResultWebSubscribe() {
  topic.inspectedResultWeb = new ROSLIB.Topic({
    ros: rosObj,
    name: "/detect_result_web",
    messageType: "yidamsg/InspectedResultWeb"
  });
  topic.inspectedResultWeb.subscribe(message => {
    if (callBackFun.inspectedResultWeb) {
      callBackFun.inspectedResultWeb(message)
    }
  });
}

/** 				
 *   巡检点结果(传给前端)取消订阅				
 *				
 */
function rosInspectedResultWebUnsubscribe() {
  if (topic.inspectedResultWeb) {
    topic.inspectedResultWeb.unsubscribe();
  }
}


/**				
 *   机器人报警结果(传给前端)声明和订阅				
 *				
 */
function rosSysAlarmWebSubscribe() {
  topic.sysAlarmWeb = new ROSLIB.Topic({
    ros: rosObj,
    name: "/sys_alarm_web",
    messageType: "yidamsg/AlarmMessage"
  });
  topic.sysAlarmWeb.subscribe(message => {
    if (callBackFun.sysAlarmWeb) {
      callBackFun.sysAlarmWeb(message)
    }
  });
}

/** 				
 *   机器人报警结果(传给前端)取消订阅				
 *				
 */
function rosSysAlarmWebUnsubscribe() {
  if (topic.sysAlarmWeb) {
    topic.sysAlarmWeb.unsubscribe();
  }
}


/**				
 *   任务状态结果(传给前端)声明和订阅				
 *				
 */
function rosTaskRealStatusWebSubscribe() {
  topic.taskRealStatusWeb = new ROSLIB.Topic({
    ros: rosObj,
    name: "/yida/robot/taskrealstatus",
    messageType: "yidamsg/TaskRealStatusWeb"
  });
  topic.taskRealStatusWeb.subscribe(message => {
    if (callBackFun.taskRealStatusWeb) {
      callBackFun.taskRealStatusWeb(message)
    }
  });
}

/** 				
 *   任务状态结果(传给前端)取消订阅				
 *				
 */
function rosTaskRealStatusWebUnsubscribe() {
  if (topic.taskRealStatusWeb) {
    topic.taskRealStatusWeb.unsubscribe();
  }
}


/**				
 *   路径规划状态返回声明和订阅				
 *				
 */
function rosTaskPlanStatusSubscribe() {
  topic.taskPlanStatus = new ROSLIB.Topic({
    ros: rosObj,
    name: "/yida/robot/task_plan_status",
    messageType: "yidamsg/taskPlanStatus"
  });
  topic.taskPlanStatus.subscribe(message => {
    if (callBackFun.taskPlanStatus) {
      callBackFun.taskPlanStatus(message)
    }
  });
}

/** 				
 *   路径规划状态返回取消订阅				
 *				
 */
function rosTaskPlanStatusUnsubscribe() {
  if (topic.taskPlanStatus) {
    topic.taskPlanStatus.unsubscribe();
  }
}


/**				
 *   控制模式状态声明和订阅				
 *				
 */
function rosControlModelStatusSubscribe() {
  topic.controlModelStatus = new ROSLIB.Topic({
    ros: rosObj,
    name: "/control_mode",
    messageType: "yidamsg/ControlMode",
    queue_size: 0
  });
  topic.controlModelStatus.subscribe(message => {
    heartStatus = 1;
    if (callBackFun.controlModelStatus) {
      callBackFun.controlModelStatus(message)
    }
  });
}

/** 				
 *   控制模式状态取消订阅				
 *				
 */
function rosControlModelStatusUnsubscribe() {
  if (topic.controlModelStatus) {
    topic.controlModelStatus.unsubscribe();
  }
}


function monitorsHeart() {
  setInterval(() => {
    if (heartStatus == 1) {
      heartStatus = 0;
    } else {
      console.log("ros heartStatus", heartStatus)
      rosObj.connect(rosUrl);
    }
  }, 3000);
}

